package com.cc.crm.web.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    private Logger log = LoggerFactory.getLogger(LoginInterceptor.class);

    //调用controll的方法前进行拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //放行情况一：如果是登录页面则放行  访问index/login(访问登录页面)  放行
        // 登录验证   user/login  放行
        if (request.getRequestURI().indexOf("login") >= 0) {
            return true;//放行
        }
        HttpSession session = request.getSession();
        //放行情况二：如果用户已登录也放行
        if (session.getAttribute("lgnUser") != null) {
            return true;
        }
        //String url = request.getScheme()+"://"+request.getServerName()+":"+
        //request.getServerPort()+request.getContextPath()+"/";
        //拦截跳转情况三：用户没有登录挑战到登录页面
        response.sendRedirect("../index/to_login");//跳转到登录页面
        return false;//拦截
    }


}
